package edu.maplef.bookwild.schema;

import java.util.HashSet;

import javax.jdo.annotations.EmbeddedOnly;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import edu.maplef.bookwild.schema.constants.NotifyArea;

/**
 * 用于本地的好友数据，需要兼顾人人和GAE两方 
 * (1)保证GAE能够使用JDO存取数据 
 * (2)保证人人抓取的信息能够迅速转换
 * 
 * @author maple
 */

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Buddy extends Base implements Comparable<Buddy> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5010857850345503089L;

	/**
	 * 图书状态类，用来存储与用户相关的图书的状态
	 * @author maple
	 */
	@PersistenceCapable
	@EmbeddedOnly
	public static class Bookship {

		/**
		 * 图书id
		 */
		@Persistent
		private String bookid;

		/**
		 * 图书状态
		 */
		@Persistent
		private String status;

		public Bookship() {
		}

		/**
		 * 带参构造函数，初始化
		 * @param bookid 图书ID
		 * @param status 图书状态
		 */
		public Bookship(String bookid, String status) {
			this.bookid = bookid;
			this.status = status;
		}

		public String getBookid() {
			return bookid;
		}

		public void setBookid(String bookid) {
			this.bookid = bookid;
		}

		public String getStatus() {
			return status;
		}

		public void setStatus(String status) {
			this.status = status;
		}

		public int hashCode() {
			return this.bookid.hashCode() + this.status.hashCode();
		}

		public boolean equals(Object obj) {
			try {
				if (this.bookid.equals(((Bookship) obj).bookid)
						&& this.status.equals(((Bookship) obj).status))
					return true;
			} catch (Exception e) {
			}
			return false;
		}
	}

	/**
	 * 用户在人人的ID
	 */
	@PrimaryKey
	private String id;

	@Persistent
	private String name;

	@Persistent
	private String headurl;

	/**
	 * 用户所拥有的图书记录
	 */
	@Persistent
	private HashSet<Bookship> bookships;

	/**
	 * 被用户放入黑名单的id：不接受各种请求,不通知新鲜事
	 */
	@Persistent
	private HashSet<String> blockids;

	/**
	 * 活动通知范围
	 */
	@Persistent
	private String notifyarea = NotifyArea.PRIVATE;

	public Buddy(int id) {
		setId(id);
	}

	/**
	 * 使用人人ID创建一个buddy对象
	 * @param id
	 */
	public Buddy(String id) {
		setId(id);
	}

	public Buddy(int id, Bookship ship) {
		this(id);
		bookships = new HashSet<Bookship>();
		bookships.add(ship);
	}

	public String getId() {
		return id;
	}

	public void setId(int id) {
		this.id = String.valueOf(id);
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getIntId() {
		try {
			return Integer.parseInt(id);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getHeadurl() {
		return headurl;
	}

	public void setHeadurl(String headurl) {
		this.headurl = headurl;
	}

	public HashSet<String> getBlockids() {
		if (blockids == null)
			blockids = new HashSet<String>();
		return blockids;
	}

	public void setBlockids(HashSet<String> blockids) {
		this.blockids = blockids;
	}

	public String getNotifyarea() {
		return notifyarea;
	}

	public void setNotifyarea(String notifyarea) {
		this.notifyarea = notifyarea;
	}

	public HashSet<Bookship> getBookships() {
		if (bookships == null)
			bookships = new HashSet<Bookship>();
		return bookships;
	}

	public void setBookships(HashSet<Bookship> bookships) {
		this.bookships = bookships;
	}

	/**
	 * add or modify or del a ownership,depending on whether it exists  
	 * @param bookship object to be operated
	 * @param isDel del the object if it is true
	 * @return true if operation goes well, false if not
	 */
	public boolean changeBookship(Bookship bookship, boolean isDel) {
		try {
			bookships = getBookships();
			if (bookships.contains(bookship)) {
				bookships.remove(bookship);
			}
			if (!isDel)
				bookships.add(bookship);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean changeBookship(String bookid, String status, boolean isDel) {
		return changeBookship(new Bookship(bookid, status), isDel);
	}

	/**
	 * block somebody from the message  
	 * @param id 
	 * @return true if operation goes well, false if not
	 */
	public boolean blockSomebody(String id) {
		try {
			blockids = getBlockids();
			if (!blockids.contains(id)) {
				blockids.add(id);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * unblock somebody from the message  
	 * @param id 
	 * @return true if operation goes well, false if not
	 */
	public boolean unblockSomebody(String id) {
		try {
			blockids = getBlockids();
			if (blockids.contains(id)) {
				blockids.remove(id);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public int compareTo(Buddy arg0) {
		try {
			if (getIntId() > arg0.getIntId())
				return 1;
			else if (getIntId() < arg0.getIntId())
				return -1;
			else
				return 0;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
}